package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableReceiveManager;
import com.stark.file.transfer.core.TransferableType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.ActivityReceiveBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;

public class ReceiveActivity extends BaseAc<ActivityReceiveBinding> implements TransferableReceiveManager.IReceiveListener {

    public static String serverIp;
    private List<SelectFileBean> listReceive = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_receive;
    }

    @Override
    protected void initView() {
        mDataBinding.ivReceiveBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvConfirm.setOnClickListener(this);
        TransferableReceiveManager manager = TransferableReceiveManager.getInstance();
        manager.setListener(this);
        manager.connectSender(serverIp);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onGetTotalReceiveCount(int totalCount) {
        mDataBinding.sbProgress.setMax(totalCount);
    }

    @Override
    public void onCompleteCount(int totalCount, int haveReceiveCount) {
        mDataBinding.sbProgress.setProgress(haveReceiveCount);
        double percent = (double) haveReceiveCount / totalCount;
        mDataBinding.tvProgress.setText((int) percent * 100 + getString(R.string.unit_percent));
        if (totalCount == haveReceiveCount) {
            mDataBinding.tvConfirm.setVisibility(View.VISIBLE);
            List<SelectFileBean> listSp = AppSpUtils.getTransferRecordList();
            if (listSp != null && listSp.size() > 0) {
                listSp.addAll(listReceive);
                AppSpUtils.saveTransferRecordList(listSp);
            } else {
                AppSpUtils.saveTransferRecordList(listReceive);
            }
        }
    }

    @Override
    public void onReceivedTransferable(@NonNull Transferable transferable) {
        SelectFileBean bean = new SelectFileBean();
        TransferableType type = transferable.getTransferType();
        if (type == TransferableType.FILE) {
            FileInfo fileInfo = (FileInfo) transferable;
            if (fileInfo.getFileType() == FileType.IMAGE) {
                bean.setType(1);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                listReceive.add(bean);
            } else if (fileInfo.getFileType() == FileType.VIDEO) {
                bean.setType(2);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                listReceive.add(bean);
            }
        } else if (type == TransferableType.CONTACT) {
            TfContactInfo tfContactInfo = (TfContactInfo) transferable;
            Log.e("test", "onReceivedTransferable: " + tfContactInfo.getContactInfoList().size());
            for (ContactInfo contactInfo : tfContactInfo.getContactInfoList()) {
                SelectFileBean contactBean = new SelectFileBean();
                contactBean.setType(3);
                contactBean.setName(contactInfo.getName());
                contactBean.setPath(contactInfo.getPhone());
                listReceive.add(contactBean);
            }
        }
    }
}